package com.kmxd.ams.app.archive.command;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.kmxd.ams.client.archive.IArchiveClassifySvc;
import com.kmxd.ams.client.archive.IArchiveFieldSvc;
import com.kmxd.ams.client.archive.IArchiveOssSettingSvc;
import com.kmxd.ams.client.archive.IArchiveVersionSvc;
import com.kmxd.ams.client.archive.dto.ArchiveFieldQueryDTO;
import com.kmxd.ams.client.archive.dto.ArchiveRuleMainAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveVersionAddDTO;
import com.kmxd.ams.client.archive.vo.ArchiveFieldListVO;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveClassify;
import com.kmxd.ams.infra.archive.entity.ArchiveOssSetting;
import com.kmxd.ams.infra.tmpl.dto.DocumentRuleConfigBO;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Component;

/**
 * 档案初始化相关
 *
 * @author ZuoLG
 */
@Slf4j
@Component
@AllArgsConstructor
public class ArchRuleInitCmd {
  private IArchiveVersionSvc archiveVersionSvc;
  private IArchiveClassifySvc archiveClassifySvc;
  private IArchiveFieldSvc archiveFieldSvc;
  private IArchiveOssSettingSvc archiveOssSettingSvc;

  /**
   * 初始化相应规则
   *
   * @param req
   */
  public void initRule(ArchiveRuleMainAddDTO req, DocumentRuleConfigBO config) {
    // 初始化归档期限及保管范围
    if (!config.getClassfyList().isEmpty()) {
      ArchiveVersionAddDTO versionAddDTO = new ArchiveVersionAddDTO();
      versionAddDTO.setId(IdWorker.getId());
      versionAddDTO.setArchiveTypeId(req.getArchTypeId());
      versionAddDTO.setFondsId(SecurityUtil.getFondsId());
      versionAddDTO.setName(Constants.DEF_VERSION_NAME);
      versionAddDTO.setCode(String.valueOf(Constants.DEF_VERSION));
      archiveVersionSvc.add(versionAddDTO);
      List<ArchiveClassify> list = Lists.newArrayList();
      config
          .getClassfyList()
          .forEach(
              item -> {
                ArchiveClassify classify = new ArchiveClassify();
                classify.setFondsId(SecurityUtil.getFondsId());
                classify.setArchiveTypeId(req.getArchTypeId());
                classify.setVersionId(versionAddDTO.getId());
                classify.setCode(item.getCode());
                classify.setName(item.getName());
                classify.setSort(item.getSort());
                list.add(classify);
              });
      archiveClassifySvc.saveBatch(list);
    }
    // 初始化附件路径
    if (!config.getFilePathList().isEmpty()) {
      List<ArchiveOssSetting> list = Lists.newArrayList();
      ArchiveFieldQueryDTO queryDTO = new ArchiveFieldQueryDTO();
      queryDTO.setArchTypeId(req.getArchTypeId());
      List<ArchiveFieldListVO> fields = archiveFieldSvc.selectList(queryDTO);
      config
          .getFilePathList()
          .forEach(
              item -> {
                if (fields.isEmpty()) {
                  log.info("当前字段{}未在指定库{}中找到!", item.getFieldCode(), req.getArchTypeId());
                } else {
                  ArchiveFieldListVO fieldVO =
                      fields.stream()
                          .filter(x -> x.getDbFieldKey().equals(item.getFieldCode()))
                          .findFirst()
                          .orElse(null);
                  if (ObjectUtil.isNotEmpty(fieldVO)) {
                    ArchiveOssSetting ossSetting = new ArchiveOssSetting();
                    ossSetting.setFondsId(SecurityUtil.getFondsId());
                    ossSetting.setArchTypeId(req.getArchTypeId());
                    ossSetting.setType(item.getType());
                    ossSetting.setSort(item.getSort());
                    ossSetting.setFieldKey(fieldVO.getDbFieldKey());
                    ossSetting.setFieldName(
                        StrUtil.format(
                            "{}({})", fieldVO.getDbFieldName(), fieldVO.getDbFieldKey()));
                    list.add(ossSetting);
                  }
                }
              });
      archiveOssSettingSvc.saveBatch(list);
    }
  }
}
